//测试完成
/*
  resolve 或者reject方法两个参数：
  context: Object => 上下文
  arguments: Array => 参数列表

*/

var Deferred = function () {
  var doneCallback = Callback('one memory'),
    failCallback = Callback('one memory'),
    state = function (state, fn) {
      console.log('您添加了' + state + '回调函数 : ' + fn)
    }
  var slice = [].slice
  var ret = {
    resolve: function () {
      doneCallback.fire(arguments[0], (slice.call(arguments, 1) || []))
    },
    reject: function () {
      failCallback.fire(arguments[0], (slice.call(arguments, 1) || []))
    },
    done: function (fn) {
      doneCallback.add(fn)
      state('成功', fn)
      return this
    },
    fail: function (fn) {
      failCallback.add(fn)
      state('失败',fn)
      return this
    },
    then: function (doneFn, failFn) {
      this.done(doneFn).fail(failFn)
      return this
    }
  }
  return ret
}